﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
namespace Web
{
    public partial class ExamPaperUpdate : System.Web.UI.Page
    {
        private int editid = 0, delid = 0, page = 1;
        private int? policyId;
        private int? subjectId;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.tbExamTime.Attributes.Add("onclick", "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});");
            if (!UserValidate.CheckLogin(this.Request, "MasterRole"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            
            if (!IsPostBack)
            {
                if (delid > 0)
                {
                    try
                    {
                        QuestionsUseNumber.DeleteQuestionsUseNumberInfo(delid);
                        Paper_Ques.DeletePaper_QuesInfoByExamPagerId(delid);
                        BAL.ExamPaper.Delete(delid);

                    }
                    catch
                    { 
                        
                    }
                    Response.Redirect("ExamPaper.aspx?page=" + page.ToString());
                    return;
                }
                if (editid > 0)
                {
                    
                    ExamPaperInfo sub = BAL.ExamPaper.GetT1(editid);
                    this.tbName.Text = sub.Name;
                    this.tbScore.Text = sub.Score.HasValue ? sub.Score.Value.ToString() : "";
                    this.tbTimeLong.Text = sub.TimeLong.HasValue ? sub.TimeLong.Value.ToString() : "";
                    this.tbExamTime.Text = sub.ExamDateTime.HasValue ? sub.ExamDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "";
                    subjectId = sub.Subject;
                    policyId = sub.TestPaperPolicyMainId;
                    this.drpUse.SelectedValue = sub.Status.ToString();
                    if (sub.Status != 0)
                    {
                        this.Button1.Enabled = false;
                        this.tbName.Enabled = false;
                        this.tbScore.Enabled = false;
                        this.tbTimeLong.Enabled = false;
                        this.tbExamTime.Enabled = false;
                        this.drpUse.Enabled = false;
                        this.drpSubject.Enabled = false;
                        this.drpPolicy.Enabled = false;
                    }
                }
                BindingSubjects();
            }
        }

        void BindingSubjects()
        {
            IList<SubjectInfo> subList = Subject.GetSubjectInfos();
            this.drpSubject.DataTextField = "Name";
            this.drpSubject.DataValueField = "Id";
            this.drpSubject.DataSource = subList;
            this.drpSubject.DataBind();
            if (subjectId.HasValue && subjectId.Value > 0)
            {
                this.drpSubject.SelectedValue = subjectId.Value.ToString();
            }
            if (this.drpSubject.SelectedValue != "")
            {
                BindingPolciy( int.Parse( this.drpSubject.SelectedValue));
            }
        }

        void BindingGrade(string subjectId)
        {
            
        }

        void BindingPolciy(int subjectId)
        {
            if (subjectId > 0)
            {
                IList<TestPaperPolicyMainInfo> subList = TestPaperPolicyMain.GetLists(subjectId);
                this.drpPolicy.DataTextField = "Name";
                this.drpPolicy.DataValueField = "Id";
                this.drpPolicy.DataSource = subList;
                this.drpPolicy.DataBind();
                if (policyId.HasValue && policyId.Value > 0)
                {
                    drpPolicy.SelectedValue = policyId.Value.ToString();
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() == "")
            {
                Utility.ShowJsAlert("请填写名称", this);
                return;
            }
            else
            {
                ExamPaperInfo sub = new ExamPaperInfo();
                int? score = null;
                int? timeLong = null;
                if (this.tbScore.Text != "")
                {
                    score = int.Parse(this.tbScore.Text);
                }
                if (this.tbTimeLong.Text != "")
                {
                    timeLong = int.Parse(this.tbTimeLong.Text);
                }
                if (this.tbExamTime.Text!="")
                {
                    sub.ExamDateTime = DateTime.Parse(this.tbExamTime.Text);
                }
                sub.Name = this.tbName.Text;
                sub.Score = score;
                sub.TimeLong = timeLong;
                sub.Status = int.Parse(this.drpUse.SelectedValue);
                if (sub.Status != 0)
                {
                    if (this.tbExamTime.Text != "")
                    {
                        sub.ExamDateTime = DateTime.Parse(this.tbExamTime.Text);
                    }
                    else
                    {
                        sub.ExamDateTime = DateTime.Now;
                    }
                }
               
                if (this.drpPolicy.SelectedValue != "")
                {
                    sub.TestPaperPolicyMainId = int.Parse(drpPolicy.SelectedValue);
                }
                if (this.drpSubject.SelectedValue != "")
                {
                    sub.Subject = int.Parse(drpSubject.SelectedValue);
                }
                if (editid > 0)
                {
                    sub.Id = editid;
                    if (sub.Status != 0) //置成使用状态
                    {
                        if (sub.ExamDateTime == null) sub.ExamDateTime = DateTime.Now;
                        QuestionsUseNumber.SaveQuestionsUseNumberInfo(sub.Id, sub.ExamDateTime.Value);

                    }
                    BAL.ExamPaper.Edit(sub);
                    Utility.ReloadTopPage(this);
                }
                else
                {
                    BAL.ExamPaper.Add(sub);
                    Utility.ReloadTopPage(this);
                }
            }
        }

        protected void drpSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //BindingGrade(this.drpSubject.SelectedValue);
        }

        protected void drpSubject_SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (drpSubject.SelectedValue != "")
            {
                BindingPolciy(int.Parse(drpSubject.SelectedValue));
            }
        }
    }
}
